﻿// Decompiled with JetBrains decompiler
// Type: StoryMode.StoryModePhases.TutorialPhase
// Assembly: StoryMode, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 11BAE65F-6C15-4628-A9C6-1B968588CDA1
// Assembly location: D:\steam\steamapps\common\Mount & Blade II Bannerlord\Modules\StoryMode\bin\Win64_Shipping_Client\StoryMode.dll

using System.Collections.Generic;
using TaleWorlds.CampaignSystem;
using TaleWorlds.CampaignSystem.Actions;
using TaleWorlds.CampaignSystem.Party;
using TaleWorlds.CampaignSystem.Roster;
using TaleWorlds.CampaignSystem.Settlements;
using TaleWorlds.Core;
using TaleWorlds.Library;
using TaleWorlds.ObjectSystem;
using TaleWorlds.SaveSystem;

#nullable disable
namespace StoryMode.StoryModePhases
{
  public class TutorialPhase
  {
    private const int GrainAmount = 3;
    private const int RecruitTroopAmount = 6;
    public const int RestrictedModePriority = 1000000;
    public const string QuestVillageStringId = "village_ES3_2";
    public const string TrainingFieldStringId = "tutorial_training_field";
    public const string RadagosRaidersStringId = "storymode_quest_raider";
    public const string TutorialVoluteerStringId = "tutorial_placeholder_volunteer";
    public const string TutorialFemaleRefugeeStringId = "storymode_quest_refugee_female";
    public const string TutorialMaleRefugeeStringId = "storymode_quest_refugee_male";
    public const string TutorialHeadmanStringId = "storymode_tutorial_headman";
    [SaveableField(1)]
    private readonly ItemRoster _tutorialPhaseShoppingRoster;

    internal static void AutoGeneratedStaticCollectObjectsTutorialPhase(
      object o,
      List<object> collectedObjects)
    {
      ((TutorialPhase) o).AutoGeneratedInstanceCollectObjects(collectedObjects);
    }

    protected virtual void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
    {
      collectedObjects.Add((object) this._tutorialPhaseShoppingRoster);
      collectedObjects.Add((object) this.TutorialFocusSettlement);
      collectedObjects.Add((object) this.TutorialFocusMobileParty);
    }

    internal static object AutoGeneratedGetMemberValueTutorialFocusSettlement(object o)
    {
      return (object) ((TutorialPhase) o).TutorialFocusSettlement;
    }

    internal static object AutoGeneratedGetMemberValueTutorialFocusMobileParty(object o)
    {
      return (object) ((TutorialPhase) o).TutorialFocusMobileParty;
    }

    internal static object AutoGeneratedGetMemberValueTalkedWithBrotherForTheFirstTime(object o)
    {
      return (object) ((TutorialPhase) o).TalkedWithBrotherForTheFirstTime;
    }

    internal static object AutoGeneratedGetMemberValueLockTutorialVillageEnter(object o)
    {
      return (object) ((TutorialPhase) o).LockTutorialVillageEnter;
    }

    internal static object AutoGeneratedGetMemberValueTutorialQuestPhase(object o)
    {
      return (object) ((TutorialPhase) o).TutorialQuestPhase;
    }

    internal static object AutoGeneratedGetMemberValueIsSkipped(object o)
    {
      return (object) ((TutorialPhase) o).IsSkipped;
    }

    internal static object AutoGeneratedGetMemberValue_tutorialPhaseShoppingRoster(object o)
    {
      return (object) ((TutorialPhase) o)._tutorialPhaseShoppingRoster;
    }

    public static TutorialPhase Instance => StoryModeManager.Current?.MainStoryLine?.TutorialPhase;

    [SaveableProperty(2)]
    public Settlement TutorialFocusSettlement { get; private set; }

    [SaveableProperty(3)]
    public MobileParty TutorialFocusMobileParty { get; private set; }

    public bool IsCompleted => this.TutorialQuestPhase == TutorialQuestPhase.Finalized;

    [SaveableProperty(5)]
    public bool TalkedWithBrotherForTheFirstTime { get; private set; }

    [SaveableProperty(6)]
    public bool LockTutorialVillageEnter { get; private set; }

    [SaveableProperty(7)]
    public TutorialQuestPhase TutorialQuestPhase { get; private set; }

    [SaveableProperty(8)]
    public bool IsSkipped { get; private set; }

    [CachedData]
    public Hero TutorialVillageHeadman { get; set; }

    public TutorialPhase()
    {
      this._tutorialPhaseShoppingRoster = new ItemRoster();
      this.TutorialFocusSettlement = (Settlement) null;
      this.TutorialFocusMobileParty = (MobileParty) null;
      this.TalkedWithBrotherForTheFirstTime = false;
      this.LockTutorialVillageEnter = false;
      this.TutorialQuestPhase = TutorialQuestPhase.None;
      this.IsSkipped = false;
    }

    public void PlayerTalkedWithBrotherForTheFirstTime()
    {
      this.TalkedWithBrotherForTheFirstTime = true;
    }

    public void SetLockTutorialVillageEnter(bool value) => this.LockTutorialVillageEnter = value;

    public void CompleteTutorial(bool isSkipped)
    {
      this.TutorialQuestPhase = TutorialQuestPhase.Finalized;
      this.RemoveTutorialFocusSettlement();
      this.RemoveTutorialFocusMobileParty();
      this.IsSkipped = isSkipped;
    }

    public void SetTutorialFocusSettlement(Settlement settlement)
    {
      this.TutorialFocusSettlement = settlement;
    }

    public void RemoveTutorialFocusSettlement() => this.TutorialFocusSettlement = (Settlement) null;

    public void RemoveTutorialFocusMobileParty()
    {
      this.TutorialFocusMobileParty = (MobileParty) null;
    }

    public void PrepareRecruitOptionForTutorial()
    {
      bool flag = 6 - (MobileParty.MainParty.MemberRoster.TotalManCount - 2) > 0;
      Hero notable = Settlement.CurrentSettlement.Notables[0];
      CharacterObject troop = MBObjectManager.Instance.GetObject<CharacterObject>("tutorial_placeholder_volunteer");
      for (int index = 0; index < notable.VolunteerTypes.Length; ++index)
      {
        if (flag || notable.VolunteerTypes[index] != null)
          notable.VolunteerTypes[index] = troop;
      }
      if (!flag)
        return;
      int amount = 6 * Campaign.Current.Models.PartyWageModel.GetTroopRecruitmentCost(troop, Hero.MainHero);
      if (Hero.MainHero.Gold >= amount)
        return;
      GiveGoldAction.ApplyBetweenCharacters((Hero) null, Hero.MainHero, amount);
    }

    public ItemRoster GetAndPrepareBuyProductsOptionForTutorial(Village village)
    {
      if (PartyBase.MainParty.ItemRoster.TotalFood <= 3)
      {
        this._tutorialPhaseShoppingRoster.AddToCounts(DefaultItems.Grain, 3);
        int amount = village.GetItemPrice(DefaultItems.Grain, (MobileParty) null, false) * 3;
        if (Hero.MainHero.Gold < amount)
          GiveGoldAction.ApplyBetweenCharacters((Hero) null, Hero.MainHero, amount);
      }
      return this._tutorialPhaseShoppingRoster;
    }

    public void SetTutorialQuestPhase(TutorialQuestPhase tutorialQuestPhase)
    {
      this.TutorialQuestPhase = tutorialQuestPhase;
    }

    public void InitializeTutorialVillageItemRoster()
    {
      Settlement settlement = MBObjectManager.Instance.GetObject<Settlement>("village_ES3_2");
      for (int index = 0; index < settlement.ItemRoster.Count; ++index)
      {
        ItemObject itemAtIndex = settlement.ItemRoster.GetItemAtIndex(index);
        if (itemAtIndex != null && !itemAtIndex.IsFood)
          this._tutorialPhaseShoppingRoster.AddToCounts(itemAtIndex, MBRandom.RandomInt(1, 4));
      }
    }
  }
}
